package com.coin900.coin.cmd.sop;

import com.google.gson.Gson;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.ReqCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.persist.sop.SopUserVO;
import com.coin900.coin.service.sop.ISopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运维人员信息修改
 *
 * @author shui
 * @create 2018-01-09
 **/
@Controller
public class SopUserPatchCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        SopUserVO sopUserVO = new Gson().fromJson(source, SopUserVO.class);
        Long id = sopUserVO.getId();
        Map<String, Object> params = new HashMap<>();
        params.put("dataStatus", 1);
        params.put("id", id);
        List<SopUserVO> sopUserVOS = this.userService.loadSopUser(params);
        if (sopUserVOS.isEmpty()) {
            respSign.setMsg("运维人员不存在");
            respSign.setRespCode(RespCode.FAIL);
            return respSign;
        }

        if (ReqCode.SOP_USER_SET_USERSTATUS.equals(sign.getReqCode())) {
            // 更改用户状态
            SopUserVO sopUser = new SopUserVO();
            sopUser.setId(id);
            sopUser.setUserStatus(sopUserVO.getUserStatus());
            sopUser.setModifiedInfor(user);
            this.userService.updateSopUser(sopUser);
            return respSign;
        }

        if (ReqCode.SOP_USER_RESET.equals(sign.getReqCode())) {
            // 重置用户
            SopUserVO sopUser = new SopUserVO();
            sopUser.setId(id);
            sopUser.setModifiedInfor(user);
            this.userService.doResetSopUser(sopUser);
            return respSign;
        }

        sopUserVO.setModifiedInfor(user);
        this.userService.updateSopUser(sopUserVO);
        return respSign;
    }

    @Autowired
    private ISopUserService userService;

    @Override
    public String getCmdCode() {
        return CmdCode.SOP_USER_PATCH;
    }
}
